package hot100;

public class ReverseList {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode reverseList(ListNode head) {
        //新建一个空头节点
        ListNode preHead = new ListNode(-1);
        //头插法
        ListNode p = head;
        while (p != null) {
            ListNode temp = p.next;
            p.next = preHead.next;
            preHead.next = p;
            p = temp;
        }

        return preHead.next;
    }
}
